package com.shqcjd.yunweiyanoperator.model.pojo;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.greendao.annotation.Convert;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.converter.PropertyConverter;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.greenrobot.greendao.annotation.Generated;

/**
 * Created by sy on 2019/5/24.
 */
@Entity
public class OfflineProblemBean {
    @Id(autoincrement = true)
    private Long id;
    private String projectName;
    private String kks;
    private String taskCode;
    private String taskId;
    private String problemInfo;
    @Convert(columnType = String.class, converter = InterConverter.class)
    private List<Integer> types;
    @Convert(columnType = String.class, converter = StringConverter.class)
    private List<String> imgs;
    private long time;
    private int loginUserId;


    @Generated(hash = 580152694)
    public OfflineProblemBean(Long id, String projectName, String kks, String taskCode,
            String taskId, String problemInfo, List<Integer> types, List<String> imgs,
            long time, int loginUserId) {
        this.id = id;
        this.projectName = projectName;
        this.kks = kks;
        this.taskCode = taskCode;
        this.taskId = taskId;
        this.problemInfo = problemInfo;
        this.types = types;
        this.imgs = imgs;
        this.time = time;
        this.loginUserId = loginUserId;
    }

    @Generated(hash = 979531794)
    public OfflineProblemBean() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getKks() {
        return this.kks;
    }

    public void setKks(String kks) {
        this.kks = kks;
    }

    public String getTaskId() {
        return this.taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getProblemInfo() {
        return this.problemInfo;
    }

    public void setProblemInfo(String problemInfo) {
        this.problemInfo = problemInfo;
    }

    public List<String> getImgs() {
        return this.imgs;
    }

    public void setImgs(List<String> imgs) {
        this.imgs = imgs;
    }

    public static class StringConverter implements PropertyConverter<List<String>, String> {

        @Override
        public List<String> convertToEntityProperty(String databaseValue) {
            if (databaseValue == null) {
                return null;
            }
            else {
                List<String> list = Arrays.asList(databaseValue.split(","));
                return list;
            }
        }

        @Override
        public String convertToDatabaseValue(List<String> entityProperty) {
            if(entityProperty==null){
                return null;
            }
            else{
                StringBuilder sb= new StringBuilder();
                for(String link:entityProperty){
                    sb.append(link);
                    sb.append(",");
                }
                return sb.toString();
            }
        }
    }
    public static class InterConverter implements PropertyConverter<List<Integer>, String> {

        private final Gson mGson;

        public InterConverter() {
            mGson = new Gson();
        }

        @Override
        public List<Integer> convertToEntityProperty(String databaseValue) {
            Type type = new TypeToken<ArrayList<Integer>>() {
            }.getType();
            ArrayList<Integer> list = mGson.fromJson(databaseValue , type);
            return list;
        }

        @Override
        public String convertToDatabaseValue(List<Integer> entityProperty) {
            String dbString = mGson.toJson(entityProperty);
            return dbString;
        }


    }

    public List<Integer> getTypes() {
        return this.types;
    }

    public void setTypes(List<Integer> types) {
        this.types = types;
    }


    public long getTime() {
        return this.time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public String getProjectName() {
        return this.projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public int getLoginUserId() {
        return this.loginUserId;
    }

    public void setLoginUserId(int loginUserId) {
        this.loginUserId = loginUserId;
    }

    @Override
    public String toString() {
        return "OfflineProblemBean{" +
                "id=" + id +
                ", projectName='" + projectName + '\'' +
                ", kks='" + kks + '\'' +
                ", taskId='" + taskId + '\'' +
                ", problemInfo='" + problemInfo + '\'' +
                ", types=" + types +
                ", imgs=" + imgs +
                ", time=" + time +
                ", loginUserId=" + loginUserId +
                '}';
    }

    public String getTaskCode() {
        return this.taskCode;
    }

    public void setTaskCode(String taskCode) {
        this.taskCode = taskCode;
    }
}
